package com.hndfsj.app.manage.web;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hndfsj.admin.domain.Dept;
import com.hndfsj.admin.domain.User;
import com.hndfsj.admin.domain.UserOperLogs;
import com.hndfsj.admin.service.IDeptService;
import com.hndfsj.admin.service.IUserOperLogsService;
import com.hndfsj.admin.service.IUserService;
import com.hndfsj.common.service.ISynchroService;
import com.hndfsj.framework.common.MReturnObject;
import com.hndfsj.framework.config.RWAdminConstant;
import com.hndfsj.framework.pager.PageRequest;
import com.hndfsj.framework.pager.SearchCondition;
import com.hndfsj.framework.security.bean.RWUserDetails;
import com.hndfsj.framework.security.context.RWAdminContext;
import com.hndfsj.framework.utils.DateUtils;

@Controller
public class SynchroCotroller {
	static Logger log = LoggerFactory.getLogger(SynchroCotroller.class);

	@Resource
	private IDeptService deptService;
	@Resource
	private IUserService userService;

	@Resource
	private IUserOperLogsService userOperLogsService;

	@Resource
	private ISynchroService synchroService;

	/**
	 * 获取全部用户
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("static-access")
	@RequestMapping(value = "/synchro/user/list")
	public @ResponseBody MReturnObject userSynchroList(HttpServletRequest request, HttpServletResponse response) {
		synchroService.saveUser(getCurrentMember(request).getUsername());
		userOperLogs("获取全部用户信息", getCurrentMember(request).getUserId());
		PageRequest pageRequest = new PageRequest();
		pageRequest.addSortConditions(User.MODIFY_TIME, pageRequest.ORDER_ASC);
		pageRequest.addAndCondition(User.USER_TYPE, SearchCondition.EQUAL, "1");
		return new MReturnObject(MReturnObject.SUCCESS, userService.getManagedAllUser(pageRequest));
	}

	/**
	 * 根据上次获取的时间 来获取这个时间段的用户
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("static-access")
	@RequestMapping(value = "/synchro/user/late/list")
	public @ResponseBody MReturnObject userSynchroLateList(HttpServletRequest request, HttpServletResponse response) {
		String modifyTime = synchroService.getSynchroUser(getCurrentMember(request).getUsername());
		userOperLogs("获取全部用户信息", getCurrentMember(request).getUserId());
		PageRequest pageRequest = new PageRequest();
		pageRequest.addSortConditions(User.MODIFY_TIME, pageRequest.ORDER_ASC);
		pageRequest.addAndCondition(User.USER_TYPE, SearchCondition.EQUAL, "1");
		if (!modifyTime.equals("")) {
			pageRequest.addAndCondition(User.MODIFY_TIME, SearchCondition.GREATER_EQUAL,
					DateUtils.formatDate(DateUtils.parseDate(modifyTime), DateUtils.DATETIME_SECOND_FORMAT));
		}
		return new MReturnObject(MReturnObject.SUCCESS, userService.getManagedAllUser(pageRequest));
	}
	/**
	 * 获取全部部门
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/synchro/dept/list")
	public @ResponseBody MReturnObject deptSynchroList(HttpServletRequest request, HttpServletResponse response) {
		synchroService.saveDept(getCurrentMember(request).getUsername());
		userOperLogs("获取全部部门信息", getCurrentMember(request).getUserId());
		return new MReturnObject(MReturnObject.SUCCESS, deptService.findAll());
	}

	/**
	 * 根据上次获取的时间 来获取这个时间段的部门
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/synchro/dept/late/list")
	public @ResponseBody MReturnObject deptSynchroLateList(HttpServletRequest request, HttpServletResponse response) {
		PageRequest pageRequest = new PageRequest();
		String modifyTime = synchroService.getSynchroDept(getCurrentMember(request).getUsername());
		if (!modifyTime.equals("")) {
			pageRequest.addAndCondition(Dept.MODIFY_TIME, SearchCondition.GREATER_EQUAL,
					DateUtils.formatDate(DateUtils.parseDate(modifyTime), DateUtils.DATETIME_SECOND_FORMAT));
		}
		userOperLogs("获取全部部门信息", getCurrentMember(request).getUserId());
		return new MReturnObject(MReturnObject.SUCCESS, deptService.findAll(pageRequest));
	}

	private void userOperLogs(String content, String userId) {
		UserOperLogs userOperLogs = new UserOperLogs();
		userOperLogs.setContent(content);
		userOperLogs.setCreateTime(new Date());
		userOperLogs.setUserId(userId);
		userOperLogsService.save(userOperLogs);
	}

	protected RWUserDetails getCurrentMember(HttpServletRequest request) {
		// 获取当前用户信息
		RWAdminContext context = (RWAdminContext) request.getSession()
				.getAttribute(RWAdminConstant.RW_ADMIN_CONTEXT_IN_SESSION);
		RWUserDetails memberDetails = context.getCurrentUser();
		return memberDetails;
	}
}
